package com.module.msg.adapter.holder

import android.view.View
import android.view.ViewGroup
import androidx.core.widget.TextViewCompat
import com.module.base.base.adapter.BaseViewHolder
import com.module.base.utils.StringUtils
import com.module.msg.R
import com.module.msg.bean.IMsgType
import com.module.msg.bean.MsgBean
import com.module.msg.databinding.MsgItemMsgBinding
import com.module.third.bean.IThirdType

class MsgHolder(parent: ViewGroup) : BaseViewHolder<MsgBean, MsgItemMsgBinding>(parent) {

    override fun initView() {
        super.initView()
        addOnClickListener(R.id.tv_support)
        addOnClickListener(R.id.tv_oppose)
        addOnClickListener(R.id.tv_jump)
    }

    override fun convert(bean: MsgBean, position: Int) {
        setStatus(bean);
        mDatabind.tvTitle.text = bean.title;
        mDatabind.tvContent.text = bean.content;
        if (bean.message.isNullOrEmpty()) {
            mDatabind.tvMessage.visibility = View.GONE;
        } else {
            mDatabind.tvMessage.visibility = View.VISIBLE;
        }
        setSupportAndOppose();
        if (bean.jump == IThirdType.IJump.NO) {
            mDatabind.tvJump.visibility = View.GONE;
        } else {
            mDatabind.tvJump.visibility = View.VISIBLE;
        }
        mDatabind.tvJump.text = bean.btnName;
    }

    fun setSupportAndOppose() {
        mDatabind.tvSupport.text = String.format(
            context.getString(R.string.msg_support_num), StringUtils.formatNumber(
                data.supportNum
            )
        )
        mDatabind.tvSupport.text = String.format(
            context.getString(R.string.msg_oppose_num), StringUtils.formatNumber(
                data.opposeNum
            )
        )
    }

    private fun setStatus(bean: MsgBean) {
        //todo 现在用户过少，暂不显示
//        mDatabind.tvOppose.setVisibility(View.VISIBLE);
//        mDatabind.tvSupport.setVisibility(View.VISIBLE);
        mDatabind.vStatus.setBackgroundResource(R.color.bg_black)
        mDatabind.vMask.visibility = View.GONE
        mDatabind.vStatus.visibility = View.VISIBLE
        when (bean.status) {
            IMsgType.IStatus.prepare -> {
                mDatabind.tvStatus.setText(R.string.msg_status_prepare)
                mDatabind.vStatus.setBackgroundResource(R.color.bg_green)
                mDatabind.tvOppose.visibility = View.INVISIBLE
                mDatabind.tvSupport.visibility = View.INVISIBLE
            }
            IMsgType.IStatus.vote -> {
                mDatabind.tvStatus.setText(R.string.msg_status_vote)
                mDatabind.vStatus.setBackgroundResource(R.color.bg_green)
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_black))
            }
            IMsgType.IStatus.start -> {
                mDatabind.tvStatus.setText(R.string.msg_status_start)
                mDatabind.vStatus.setBackgroundResource(R.color.bg_red)
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_black))
            }
            IMsgType.IStatus.end -> {
                mDatabind.tvStatus.setText(R.string.msg_status_end)
                mDatabind.vMask.visibility = View.VISIBLE
                mDatabind.vStatus.setBackgroundResource(R.color.bg_gray)
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_gray))
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_gray))
            }
            IMsgType.IStatus.discard -> {
                mDatabind.tvStatus.setText(R.string.msg_status_discard)
                mDatabind.vMask.visibility = View.VISIBLE
                mDatabind.vStatus.setBackgroundResource(R.color.bg_gray)
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_gray))
                mDatabind.tvStatus.setTextColor(context.resources.getColor(R.color.bg_gray))
            }
            IMsgType.IStatus.notice -> {
                mDatabind.tvStatus.setText(R.string.msg_status_notice)
                mDatabind.vStatus.visibility = View.GONE
                TextViewCompat.setTextAppearance(mDatabind.tvTitle, R.style.TextItemTitle)
            }
            IMsgType.IStatus.activity -> {
                mDatabind.tvStatus.setText(R.string.msg_status_activity)
                mDatabind.vStatus.visibility = View.GONE
                TextViewCompat.setTextAppearance(mDatabind.tvTitle, R.style.TextItemTitle)
            }
        }
    }

}